package com.yitao.cms.service.order;

import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.order.MallOrderMoneyChangeLogDto;
import com.yitao.client.model.order.MallOrderModel;
import com.yitao.client.model.order.MallOrderMoneyChangeLogModel;
import com.yitao.client.model.order.QMallOrderMoneyChangeLogModel;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.Date;
import java.util.List;

@Service
public class CmsOrderMoneyChangeLogService extends GenericEntityService<MallOrderMoneyChangeLogModel, MallOrderMoneyChangeLogDto, QMallOrderMoneyChangeLogModel> {
    public CmsOrderMoneyChangeLogService(EntityManager entityManager) {
        super(entityManager);
    }

    public List<MallOrderMoneyChangeLogModel> gMallOrderMoneyChangeLogByOrderId(Long orderId) {
        return queryFactory.select(Q).from(Q).where(Q.isDelete.eq(false).and(Q.orderId.eq(orderId))).orderBy(Q.createTime.desc()).fetch();

    }

    @Transactional
    public void saveMallOrderMoneyChangeLogModel(MallOrderModel mallOrderModel,
                                                 Long endAmount,
                                                 OperationUserCmsModel userSidFromRedis,
                                                 Date date, Long orderDetailId) {
        MallOrderMoneyChangeLogModel mallOrderMoneyChangeLogModel = new MallOrderMoneyChangeLogModel();
        mallOrderMoneyChangeLogModel.setOrderDetailId(orderDetailId);
        mallOrderMoneyChangeLogModel.setBaseAmount(mallOrderModel.getOrderTotalAmount());
        mallOrderMoneyChangeLogModel.setStartAmount(mallOrderModel.getActualAmount());
        mallOrderMoneyChangeLogModel.setEndAmount(endAmount);
        mallOrderMoneyChangeLogModel.setCreateBy(userSidFromRedis.getId());
        mallOrderMoneyChangeLogModel.setCreateTime(date);
        mallOrderMoneyChangeLogModel.setCreateByPhone(userSidFromRedis.getPhone());
        mallOrderMoneyChangeLogModel.setOrderCode(mallOrderModel.getOrderCode());
        mallOrderMoneyChangeLogModel.setOrderId(mallOrderModel.getId());
        mallOrderMoneyChangeLogModel.setIsDelete(false);
        save(mallOrderMoneyChangeLogModel);
    }

    @Transactional
    public void save(MallOrderMoneyChangeLogModel mallOrderMoneyChangeLogModel) {
        repository.save(mallOrderMoneyChangeLogModel);
    }
}
